package com.ledim.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.LedimChoiceCardBean;
import com.ledim.utils.ConstantUtil;
import com.ledim.utils.FrescoUtil;
import com.ledim.utils.LedimBaseUtils;
import com.ledim.utils.LedimFunctionUtils;
import java.util.Iterator;
import java.util.List;

/**
 * Created by ccx on 16/6/1.
 * 卡片adapter基类
 */
public abstract class CardBaseAdapter extends LedimBaseListAdapter<LedimChoiceCardBean> {

    protected String groupId;

    public CardBaseAdapter(Context c, List dataList, String groupId) {
        super(c);
        this.groupId = groupId;
        setDatas(dataList);
    }

    public CardBaseAdapter(Context c, String groupId) {
        super(c);
        this.groupId = groupId;
    }

    public abstract static class CardBaseViewHolder {

        SimpleDraweeView mPhoto;
        TextView mTitle;
        TextView mRating;
        TextView mEpisodesCount;
        TextView mSubTitle;

        public CardBaseViewHolder(View view) {
            initHoldView(view);
        }

        public abstract void initHoldView(View view);

    }

    protected void initCommonView(View convertView, CardBaseViewHolder holder, int position) {
        final LedimChoiceCardBean mCard = getItem(position);
        if (mCard.link.lastIndexOf("/") + 1 == mCard.link.length()) {
            mCard.link = mCard.link + "0";
        }
        if (holder.mSubTitle != null) {
            holder.mSubTitle.setText(Html.fromHtml(mCard.subtitle));
        }
        if (mCard.photo != null && !TextUtils.isEmpty(mCard.photo.thumb) && holder.mPhoto != null) {
            FrescoUtil.loadUrl(mCard.photo.thumb, holder.mPhoto);
        }
        if (holder.mTitle != null) {
            holder.mTitle.setText(Html.fromHtml(mCard.title.trim()));
        }
        if (mCard.simple_album != null && mCard.simple_album.category != null) {
            Iterator<Integer> iterator = mCard.simple_album.category.keySet().iterator();
            int albumType = -1;
            while (iterator.hasNext()) {
                albumType = iterator.next();
            }
            if (holder.mRating != null) {
                if (albumType == ConstantUtil.ChannelType.film) {
                    holder.mRating.setVisibility(View.VISIBLE);
                    holder.mRating.setText(LedimBaseUtils.formatRating(mCard.simple_album.rating));
                } else {
                    holder.mRating.setVisibility(View.GONE);
                }
            }
            if (holder.mEpisodesCount != null) {
                if (albumType == ConstantUtil.ChannelType.film) {
                    holder.mEpisodesCount.setVisibility(View.GONE);
                } else if (albumType == ConstantUtil.ChannelType.tv ||
                        albumType == ConstantUtil.ChannelType.cartoon) {
                    holder.mEpisodesCount.setVisibility(View.VISIBLE);
                    if (mCard.simple_album.is_end == 1) {
                        holder.mEpisodesCount.setText(mCard.simple_album.episode + "集全");
                    } else {
                        holder.mEpisodesCount.setText(
                                "更新至第" + mCard.simple_album.now_episodes + "集");
                    }
                } else if (albumType == ConstantUtil.ChannelType.variety
                    /*|| albumType == ENUM_CHANNEL_TYPE.SPORT.value()  //暂时不加体育 */) {
                    holder.mEpisodesCount.setVisibility(View.VISIBLE);

                    if (mCard.simple_album.is_end == 1) {
                        //					int episodeCount = Integer.parseInt(getEpisodes(mCard.simple_album.episode));
                        //                    if(episodeCount>0){
                        holder.mEpisodesCount.setText(
                                getEpisodes(mCard.simple_album.episode) + "期全");
                        //                     }
                    } else {
                        //					int nowIssueCount = Integer.parseInt(getEpisodes(mCard.simple_album.now_issue));
                        //					if (nowIssueCount>0) {
                        holder.mEpisodesCount.setText(
                                getEpisodes(mCard.simple_album.now_issue) + "期");
                        //				}
                    }

                } else if (albumType == ConstantUtil.ChannelType.feed ||
                        albumType == ConstantUtil.ChannelType.music ||
                        albumType == ConstantUtil.ChannelType.entertainment) {
                    holder.mEpisodesCount.setVisibility(View.VISIBLE);
                    holder.mEpisodesCount.setText(LedimBaseUtils.secToTime(mCard.simple_album.duration));
                } else {
                    holder.mEpisodesCount.setVisibility(View.GONE);
                }
            }
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LedimFunctionUtils.jumpTargetActivity((Activity) context, mCard, groupId);
            }
        });
    }

    private String getEpisodes(String str) {
        String strEnd;
        if (str.length() == 8) {
            strEnd = str.substring(0, 4) + "-" + str.substring(4, 6) + "-" +
                    str.substring(6, str.length());
        } else {
            strEnd = str;
        }
        return strEnd;
    }

}
